package com.jh.hts_jdk.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class IO {

    private static InputStreamReader GetFileStreamFromJar(String s) {
        return new InputStreamReader(IO.class.getClassLoader().getResourceAsStream(s));
    }

    private static FileReader GetFileStreamFromIDE(String s) throws FileNotFoundException {
        return new FileReader(IO.class.getClassLoader().getResource(s).getPath());
    }

    public static void ExtractFile(String InternalFile, File OutFile) throws IOException {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(GetFileStreamFromIDE(InternalFile));
        } catch (Exception e) {
            reader = new BufferedReader(GetFileStreamFromJar(InternalFile));
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(OutFile));
        String Line;
        while ((Line = reader.readLine()) != null) {
            writer.write(Line + "\n");
        }
        writer.close();
    }
}
